package cn.stylefeng.guns.modular.business.controller;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.business.entity.SubDic;
import cn.stylefeng.guns.modular.business.pojo.SubDicRequest;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@ApiResource(name="首页业务controller")
@RestController
@RequestMapping("/yunhe")
public class YunheController {
    @Resource
    private EventsInfoService eventsInfoService;
    @Resource
    private RiverInfoService riverInfoService;
    @Resource
    private ProjectScienceService projectScienceService;
    @Resource
    private DocumentInfoService documentInfoService;
    @Resource
    private TransportInfoService transportInfoService;
    @Resource
    private SubFileInfoService subFileInfoService;
    @Resource
    private SubDicService subDicService;

    @Resource
    private SubDrawInfoService drawInfoService;
    @Resource
    private CultureInfoService cultureInfoService;
    @Resource
    private TownInfoService townInfoService;
    @Resource
    private HeritageInfoService heritageInfoService;
    @Resource
    private FigureInfoService figureInfoService;
    @Resource
    private FigureEventService figureEventService;
    @Resource
    private CyclopediaInfoService cyclopediaInfoService;
    @Resource
    private ResourceInfoService resourceInfoService;

    private ExecutorService executor = Executors.newCachedThreadPool() ;

    public void fun() throws Exception {

        executor.submit(new Runnable(){

            @Override

            public void run() {

                try {
                    //要执行的业务代码，我们这里没有写方法，可以让线程休息几秒进行测试

                    Thread.sleep(10000);

                    System.out.print("睡够啦~");

                }catch(Exception e) {

                    throw new RuntimeException("报错啦！！");

                }

            }

        });

    }
    @GetResource(path="/findAll")
    public ResponseData findAll(String name,String fingType,String river,String time,String town) {
        PageResult<Object> objectPageResult = new PageResult<>();
        if("".equals(fingType)) {
            fingType = "0;1;2;3";
        }
        String[] types = StrUtil.splitToArray(fingType, ";");
//        List<String> stringList1 = StrUtil.splitToArray(fingType, ";");
//        String [] types = stringList1.toArray(new String[stringList1.size()]);
       // PageResult<EventsInfo> pageResult= new PageResult<>();
        //大事记
        List<Map<String,Object>> eventPageResult = new ArrayList<>();
        //河道
        List<Map<String,Object>> riverPageResult = new ArrayList<>();
        //工程科技
        List<Map<String,Object>> projectPageResult = new ArrayList<>();
        //文献
        List<Map<String,Object>> docResult = new ArrayList<>();
        //漕运
        List<Map<String,Object>> transportResult = new ArrayList<>();

        //图志
        List<Map<String,Object>> drawInfoResult = new ArrayList<>();
        //文化
        List<Map<String,Object>> cultureInfoResult = new ArrayList<>();
        //城镇
        List<Map<String,Object>> townInfoResult = new ArrayList<>();
        //遗产考古
        List<Map<String,Object>> heritageInfoResult = new ArrayList<>();
        //人物故事
        List<Map<String,Object>> figureInfoResult = new ArrayList<>();
        //人物事件
        List<Map<String,Object>> figureEventResult = new ArrayList<>();
        //百科
        List<Map<String,Object>> cyclopediaInfoResult = new ArrayList<>();
        //资讯
        List<Map<String,Object>> resourceInfoResult = new ArrayList<>();

        for(int j=0; j<types.length;j++){
            if("0".equals(types[j])){
                String [] times = StrUtil.splitToArray(time, ";");
                List<SubDic> riverList = new ArrayList<>();
                List<SubDicRequest> subDicRequestList = new ArrayList<>();
                if(!"".equals(river)) {

                    String [] strings = StrUtil.splitToArray(river, ";");
                    for(int i=0; i<strings.length;i++){
                        SubDicRequest subDicRequest = new SubDicRequest();
                        subDicRequest.setDicZname(strings[i]);
                        subDicRequest.setDicType(2L);
                        subDicRequestList.add(subDicRequest);
                    }
                    riverList = subDicService.findAllList(subDicRequestList);

                }
                List<SubDic> townList = new ArrayList<>();
                List<SubDicRequest> subDicRequestList1 = new ArrayList<>();
                if(!"".equals(town)) {
                    List<String> stringLists = StrUtil.split(town, ";");
                    String [] strings = stringLists.toArray(new String[stringLists.size()]);
                    for(int i=0; i<strings.length;i++){
                        SubDicRequest subDicRequest = new SubDicRequest();
                        subDicRequest.setDicZname(strings[i]);
                        subDicRequest.setDicType(1L);
                        subDicRequestList1.add(subDicRequest);
                    }
                    townList = subDicService.findAllList(subDicRequestList1);

                }


                eventPageResult = eventsInfoService.findAllList(riverList,townList,times,name);
                riverPageResult = riverInfoService.findAllList(riverList,townList,times,name);
                projectPageResult = projectScienceService.findAllList(riverList,townList,times,name);
                docResult = documentInfoService.findAllList(riverList,townList,times,name);
                transportResult = transportInfoService.findAllList(riverList,townList,times,name);

                drawInfoResult = drawInfoService.findAllListDraw(riverList,townList,times,name);
                cultureInfoResult = cultureInfoService.findAllList(riverList,townList,times,name);
                townInfoResult = townInfoService.findAllList(riverList,townList,times,name);
                heritageInfoResult = heritageInfoService.findAllList(riverList,townList,times,name);
                figureInfoResult = figureInfoService.findAllList(riverList,townList,times,name);
                figureEventResult = figureEventService.findAllList(riverList,townList,times,name);
                cyclopediaInfoResult = cyclopediaInfoService.findAllList(riverList,townList,times,name);
                resourceInfoResult = resourceInfoService.findAllList(riverList,townList,times,name);
            }
        }

        //PageResult<SubFileInfo> subFileInfoPageResult = subFileInfoService.findAllPage(types,name);
        List<Map<String,Object>> subFileInfoPageResult = new ArrayList<>();
        if( river==""&&time==""&& town=="") {
             subFileInfoPageResult = subFileInfoService.findAllInfo(types, name);
        }
        List<Object> objects = new ArrayList<>();
        objects.addAll(eventPageResult);
        objects.addAll(riverPageResult);
        objects.addAll(projectPageResult);
        objects.addAll(subFileInfoPageResult);
        objects.addAll(docResult);
        objects.addAll(transportResult);

        objects.addAll(drawInfoResult);
        objects.addAll(cultureInfoResult);
        objects.addAll(townInfoResult);
        objects.addAll(heritageInfoResult);
        objects.addAll(figureInfoResult);
        objects.addAll(figureEventResult);
        objects.addAll(cyclopediaInfoResult);
        objects.addAll(resourceInfoResult);
//        objectPageResult.setPageNo(pageResult.getPageNo());
//        objectPageResult.setPageSize(pageResult.getPageSize()+subFileInfoPageResult.getPageSize());
//        Integer totalPage = (pageResult.getTotalRows()+subFileInfoPageResult.getTotalRows())/(pageResult.getPageSize()+subFileInfoPageResult.getPageSize()) +1;
//        objectPageResult.setTotalPage(totalPage);
//        objectPageResult.setTotalRows(pageResult.getTotalRows()+subFileInfoPageResult.getTotalRows());
//        objectPageResult.setRows(objects);
        Page<Object> page = PageFactory.defaultPage();
        Page<Object> pages = new Page<>(page.getCurrent(),page.getSize());
        int start =(int)((page.getCurrent()-1)*page.getSize());
        int end =(int)((page.getCurrent())*page.getSize());
        if(objects.size()<end){
            end = objects.size();
        }
        List<Object> t2 = objects.subList(start,end);
        pages.setRecords(t2);
        pages.setTotal(objects.size());
        PageResult<Object> pageResults =  PageResultFactory.createPageResult(pages);

        return new SuccessResponseData(pageResults);

    }


    @GetResource(path="/findAllByApp")
    public ResponseData findAllByApp(String name,String fingType,String river,String time,String town,String code) {
        PageResult<Object> objectPageResult = new PageResult<>();
        if("".equals(fingType)) {
            fingType = "0";
        }
//        String[] types = StrUtil.split(fingType, ";");

        String [] types =StrUtil.splitToArray(fingType, ";");
        // PageResult<EventsInfo> pageResult= new PageResult<>();
        //大事记
        List<Map<String,Object>> eventPageResult = new ArrayList<>();
        //河道
        List<Map<String,Object>> riverPageResult = new ArrayList<>();
        //工程科技
        List<Map<String,Object>> projectPageResult = new ArrayList<>();
        //文献
        List<Map<String,Object>> docResult = new ArrayList<>();
        //漕运
        List<Map<String,Object>> transportResult = new ArrayList<>();
        //图志
        List<Map<String,Object>> drawInfoResult = new ArrayList<>();
        //文化
        List<Map<String,Object>> cultureInfoResult = new ArrayList<>();
        //城镇
        List<Map<String,Object>> townInfoResult = new ArrayList<>();
        //遗产考古
        List<Map<String,Object>> heritageInfoResult = new ArrayList<>();
        //人物故事
        List<Map<String,Object>> figureInfoResult = new ArrayList<>();
        //人物事件
        List<Map<String,Object>> figureEventResult = new ArrayList<>();
        //百科
        List<Map<String,Object>> cyclopediaInfoResult = new ArrayList<>();
        //资讯
        List<Map<String,Object>> resourceInfoResult = new ArrayList<>();

        for(int j=0; j<types.length;j++){
            if("0".equals(types[j])){
                String [] times = StrUtil.splitToArray(time,";");
//                List<String> stringList1 = StrUtil.split(fingType, ";");
//                String [] types = stringList1.toArray(new String[stringList1.size()]);
                List<SubDic> riverList = new ArrayList<>();
                List<SubDicRequest> subDicRequestList = new ArrayList<>();
                if(!"".equals(river)) {

                    String [] strings = StrUtil.splitToArray(river,";");
                    for(int i=0; i<strings.length;i++){
                        SubDicRequest subDicRequest = new SubDicRequest();
                        subDicRequest.setDicZname(strings[i]);
                        subDicRequest.setDicType(2L);
                        subDicRequestList.add(subDicRequest);
                    }
                    riverList = subDicService.findAllList(subDicRequestList);

                }
                List<SubDic> townList = new ArrayList<>();
                List<SubDicRequest> subDicRequestList1 = new ArrayList<>();
                if(!"".equals(town)) {
                    String [] strings = StrUtil.splitToArray(town,";");
                    for(int i=0; i<strings.length;i++){
                        SubDicRequest subDicRequest = new SubDicRequest();
                        subDicRequest.setDicZname(strings[i]);
                        subDicRequest.setDicType(1L);
                        subDicRequestList1.add(subDicRequest);
                    }
                    townList = subDicService.findAllList(subDicRequestList1);

                }

                if("".equals(code)){
                    eventPageResult = eventsInfoService.findAllList(riverList,townList,times,name);
                    riverPageResult = riverInfoService.findAllList(riverList,townList,times,name);
                    projectPageResult = projectScienceService.findAllList(riverList,townList,times,name);
                    docResult = documentInfoService.findAllList(riverList,townList,times,name);
                    transportResult = transportInfoService.findAllList(riverList,townList,times,name);

                    drawInfoResult = drawInfoService.findAllListDraw(riverList,townList,times,name);
                    cultureInfoResult = cultureInfoService.findAllList(riverList,townList,times,name);
                    townInfoResult = townInfoService.findAllList(riverList,townList,times,name);
                    heritageInfoResult = heritageInfoService.findAllList(riverList,townList,times,name);
                    figureInfoResult = figureInfoService.findAllList(riverList,townList,times,name);
                    figureEventResult = figureEventService.findAllList(riverList,townList,times,name);
                    cyclopediaInfoResult = cyclopediaInfoService.findAllList(riverList,townList,times,name);
                    resourceInfoResult = resourceInfoService.findAllList(riverList,townList,times,name);
                }else {

                    if ("YHSJ".equals(code)) {
                        eventPageResult = eventsInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("HDHZ".equals(code)) {
                        riverPageResult = riverInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("GCKJ".equals(code)) {
                        projectPageResult = projectScienceService.findAllList(riverList, townList, times, name);
                    }
                    if ("WXDJ".equals(code)) {
                        docResult = documentInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("CYHY".equals(code)) {
                        transportResult = transportInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("YHTZ".equals(code)) { //图
                        drawInfoResult = drawInfoService.findAllListDraw(riverList, townList, times, name);
                    }
                    if ("YHWH".equals(code)) {
                        cultureInfoResult = cultureInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("YHCZ".equals(code)) {
                        townInfoResult = townInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("YCKG".equals(code)) {
                        heritageInfoResult = heritageInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("RWGS".equals(code)) {
                        figureInfoResult = figureInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("RWGS".equals(code)) {
                        figureEventResult = figureEventService.findAllList(riverList, townList, times, name);
                    }
                    if ("YHBK".equals(code)) {
                        cyclopediaInfoResult = cyclopediaInfoService.findAllList(riverList, townList, times, name);
                    }
                    if ("YHZX".equals(code)) { //资讯
                        resourceInfoResult = resourceInfoService.findAllList(riverList, townList, times, name);
                    }
                }
            }
        }

        //PageResult<SubFileInfo> subFileInfoPageResult = subFileInfoService.findAllPage(types,name);
        List<Map<String,Object>> subFileInfoPageResult = new ArrayList<>();
        if(code.isEmpty()){
            if( river==""&&time==""&& town=="") {
                subFileInfoPageResult = subFileInfoService.findAllInfo(types, name);
            }
        }

        List<Object> objects = new ArrayList<>();
        objects.addAll(eventPageResult);
        objects.addAll(riverPageResult);
        objects.addAll(projectPageResult);
        objects.addAll(subFileInfoPageResult);
        objects.addAll(docResult);
        objects.addAll(transportResult);

        objects.addAll(drawInfoResult);
        objects.addAll(cultureInfoResult);
        objects.addAll(townInfoResult);
        objects.addAll(heritageInfoResult);
        objects.addAll(figureInfoResult);
        objects.addAll(figureEventResult);
        objects.addAll(cyclopediaInfoResult);
        objects.addAll(resourceInfoResult);
//        objectPageResult.setPageNo(pageResult.getPageNo());
//        objectPageResult.setPageSize(pageResult.getPageSize()+subFileInfoPageResult.getPageSize());
//        Integer totalPage = (pageResult.getTotalRows()+subFileInfoPageResult.getTotalRows())/(pageResult.getPageSize()+subFileInfoPageResult.getPageSize()) +1;
//        objectPageResult.setTotalPage(totalPage);
//        objectPageResult.setTotalRows(pageResult.getTotalRows()+subFileInfoPageResult.getTotalRows());
//        objectPageResult.setRows(objects);
        Page<Object> page = PageFactory.defaultPage();
        Page<Object> pages = new Page<>(page.getCurrent(),page.getSize());
        int start =(int)((page.getCurrent()-1)*page.getSize());
        int end =(int)((page.getCurrent())*page.getSize());
        if(objects.size()<end){
            end = objects.size();
        }
        List<Object> t2 = objects.subList(start,end);
        pages.setRecords(t2);
        pages.setTotal(objects.size());
        PageResult<Object> pageResults =  PageResultFactory.createPageResult(pages);
        return new SuccessResponseData(pageResults);

    }

}
